/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.converter.vm;

import com.cgmnx.admin.common.util.DateUtil;
import com.cgmnx.admin.dao.entity.rbac.OperationLogEntity;
import com.cgmnx.admin.model.enums.rbac.OperationType;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.vm.OperationLogVm;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author: created  Date: 16/12/21 Time: 下午4:22
 * @version: 1.0
 */
@Component
public class OperationVmConverter implements Convert<OperationLogEntity, OperationLogVm> {

    @Override
    public OperationLogVm convert(OperationLogEntity t) {

        if (t == null) {
            return null;
        }

        OperationLogVm vm = new OperationLogVm();
        vm.setId(t.getId());
        vm.setContent(t.getContent());
        vm.setIp(t.getIp());
        vm.setOperationId(t.getOperationId());
        vm.setOperatorName(t.getOperatorName());

        OperationType operationType = t.getOperationType();
        if (operationType != null) {
            vm.setOperationTypeCode(operationType.getCode());
            vm.setOperationTypeText(operationType.getName());
        }
        vm.setUpdateUser(t.getUpdateUser());
        vm.setCreateUser(t.getCreateUser());
        vm.setCreateTime(DateUtil.formatDatetime(t.getCreateTime()));
        vm.setUpdateTime(DateUtil.formatDatetime(t.getUpdateTime()));
        return vm;
    }

    @Override
    public List<OperationLogVm> convert(List<OperationLogEntity> t) {

        if (CollectionUtils.isEmpty(t)) {
            return Collections.emptyList();
        }

        return FluentIterable.from(t).transform(new Function<OperationLogEntity, OperationLogVm>() {

            @Override
            public OperationLogVm apply(OperationLogEntity input) {

                if (input == null) {
                    return null;
                }

                return convert(input);
            }
        }).toList();
    }
}